package com.liubo.ssepro.Controller;


import com.liubo.ssepro.Entity.NewuserEntity;
import com.liubo.ssepro.Entity.TokenEntity;
import com.liubo.ssepro.Entity.UserEntity;
import com.liubo.ssepro.Repository.TokenRepository;
import com.liubo.ssepro.Repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class AppController {
    //login
    @Autowired
    UserRepository userRepository;
    @Autowired
    TokenRepository tokenRepository;
    //login GET
    @RequestMapping(value = {"/","login"}, method = RequestMethod.GET)
    public String logging( ){
        return "login";
    }


    //login POST
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public String logging(HttpServletRequest request,Model model){
        String phone = request.getParameter("name");
        String pawd = request.getParameter("password");
        //int id = Integer.parseInt(ids);
        // TODO: 如果找不到手机号，那么，直接return
        String msg = "成功";
        UserEntity userEntity = userRepository.findByPhone(phone);
        if(userEntity == null){
            msg = "您的帐号不存在！";
            model.addAttribute("msg",msg);
            model.addAttribute("phone",phone);
            return "login";
        }
        String password = userEntity.getPassword();
        if(password.equals(pawd)/* && userEntity.getSustutas()*/) {
            /*
            生成token
             */
            //TokenEntity tokenEntity = tokenRepository.findByPhone(phone);
            //tokenEntity.setSuat(new Date());
            //tokenEntity.setMaptoken(tokenEntity.hashCode()+"");
           // tokenRepository.updateMaptoken(phone,tokenEntity.hashCode()+"",new Date());

            return "create";
        }
        msg = "您的帐号或者密码错误！";
        model.addAttribute("msg",msg);
        model.addAttribute("phone",phone);
        return "login";
    }
    /*
    create  GET
     */

    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String createUser( ){
        return "create";
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String createUser(@Valid NewuserEntity newuserEntity, BindingResult bindingResult) throws ParseException {
        if(bindingResult.hasErrors()){
            System.out.println(bindingResult.getFieldError().getDefaultMessage());
            return "create";
        }
        String name = newuserEntity.getName();

        String password = newuserEntity.getPassword();

        String sexs = newuserEntity.getSex();
        boolean sex = true;
        if (sexs.equals("男") ){
            sex = true;
        }
        else {
            sex = false;
        }

        String phone = newuserEntity.getPhone();

        String births = newuserEntity.getBirth();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date birth = sdf.parse(births);

        String address = newuserEntity.getAddress();

        String idrecommends = newuserEntity.getIdrecommend();
        int idrecommend = Integer.parseInt(idrecommends);


        String idcommittees = newuserEntity.getIdcommittee();
        //Integer idcommittee = Integer.parseInt(idcommittees);

        Date createat = new Date();

        UserEntity userEntity =new UserEntity();
        userEntity.setBirth(birth);
        userEntity.setIdcisc(1);
        userEntity.setIdcommittee(1);
        userEntity.setIdrecommend(idrecommend);
        userEntity.setName(name);
        userEntity.setPassword(password);
        userEntity.setPhone("12131564");
        userEntity.setSex(sex);
        userEntity.setPassstatus(false);
        userEntity.setCreatat(createat);
        userEntity.setSustutas(false);
        userEntity.setAddress(address);

        userRepository.saveAndFlush(userEntity);
        /*
        生成token库
         */
        TokenEntity tokenEntity = new TokenEntity();
        tokenEntity.setName(name);
        tokenEntity.setPhone(phone);
        tokenEntity.setPassword(password);
        tokenRepository.saveAndFlush(tokenEntity);

        return "login";
    }
    //create   POST
    /*@RequestMapping(value = "/create", method = RequestMethod.POST)
    public String createUser(HttpServletRequest request) throws ParseException {

        String name = request.getParameter("name");

        String password = request.getParameter("password");

        String sexs = request.getParameter("sex");
        boolean sex = true;
        if (sexs.equals("男") ){
            sex = true;
        }
        else {
            sex = false;
        }

        String phone = request.getParameter("phone");

        String births = request.getParameter("birth");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date birth = sdf.parse(births);

        String address = request.getParameter("address");

        String idrecommends = request.getParameter("idrecommend");
        int idrecommend = Integer.parseInt(idrecommends);

        String idciscs = request.getParameter("idcisc");
        //Integer idcisc = Integer.parseInt(idciscs);

        String idcommittees = request.getParameter("idcommittee");
        //Integer idcommittee = Integer.parseInt(idcommittees);

        Date createat = new Date();

        UserEntity userEntity =new UserEntity();
        userEntity.setBirth(birth);
        userEntity.setIdcisc(1);
        userEntity.setIdcommittee(1);
        userEntity.setIdrecommend(idrecommend);
        userEntity.setName(name);
        userEntity.setPassword(password);
        userEntity.setPhone("12131564");
        userEntity.setSex(sex);
        userEntity.setPassstatus(false);
        userEntity.setCreatat(createat);
        userEntity.setSustutas(false);
        userEntity.setAddress(address);

        userRepository.saveAndFlush(userEntity);

        return "create";
    }*/

    @RequestMapping("/hello")
    public String index(ModelMap map) {
        // 加入一个属性，用来在模板中读取
        map.addAttribute("host", "http://blog.didispace.com");
        map.addAttribute("name", "  6666");
        // return模板文件的名称，对应src/main/resources/templates/index.html
        return "create";
    }

}
